<!-- *Author: 黄旗, *Date: 2020-08-04 15:45:59 -->
<template>
    <Modal :width="600" v-model='modal' title='修改密码' transfer :mask-closable='false' @on-cancel='canceledit(modalname)'>
        <Form :label-width='130' :ref='modalname' :model='modalparams' :rules='createRules'>
            <FormItem label='友情提示：'>
                <p class="red">默认用户初始密码：jgwl123456</p>
            </FormItem>
            <FormItem label='密码：' prop='password'>
                <Input style="width: 280px" clearable v-model='modalparams.password' placeholder='默认为空，代表不修改密码' />
            </FormItem>
        </Form>
        <div slot='footer'>
            <Button @click='canceledit(modalname)'>取消</Button>
            <Button type='primary' :loading="subloading" @click='submitedit(modalname)'>保存</Button>
        </div>
    </Modal>
</template>

<script>
    import { rbacmemberchangepassword } from '@api/peoplesetting'
    import util from '@/libs/util'
    import router from '@/router';
    export default {
        props: ['condition'],
        name: '',
        data () {
            return {
                modalname: 'modalname',
                modal: false,
                subloading: false,
                modalparams: { id: '', password: '' },
                createRules: {
                    password: [{ required: true, message: '请输入新密码', trigger: 'blur' }]
                }
            };
        },
        /* 函数方法集合 */
        methods: {
            /* @函数功能: 初始化 */
            initial () {
                this.modalparams.id = util.cookies.get('uuid');
                this.modal = true
                this.$refs[this.modalname].resetFields();
            },
            canceledit (name) {
                this.$refs[name].resetFields();
                this.modal = false
            },
            submitedit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.subloading = true
                        rbacmemberchangepassword(this.modalparams).then(() => {
                            this.$Message.success('修改成功')
                            util.cookies.remove('token');
                            util.cookies.remove('uuid');
                            // 密码存储到cookies中base64加密存储
                            // let loginInfo = util.storage.get('loginInfo')
                            let password= util.cookies.get('password');
                            let autoLogin = util.storage.get('autoLogin')
                            util.storage.clear()
                            util.cookies.set('password',password);
                            // util.storage.set('loginInfo', loginInfo)
                            util.storage.set('autoLogin', autoLogin)
                            router.push({
                                name: 'login'
                            });
                            this.subloading = false
                            this.modal = false
                        }).catch(() => {
                            this.subloading = false
                        })
                    }
                })
            }
        }
    }
</script>
